package com.autonavi.yunda.yunji.core.vo.response;

import com.autonavi.yunda.yunji.common.exception.AmapPreconditions;
import com.autonavi.yunda.yunji.common.utils.JsonUtils;
import com.autonavi.yunda.yunji.core.model.dto.ProjectDTO;
import com.autonavi.yunda.yunji.core.vo.ProjectExtraData;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import org.apache.logging.log4j.util.Strings;
import org.springframework.util.DigestUtils;

import java.sql.Timestamp;
import java.util.Map;

/**
 * @author cizhong.wcs
 * @date 2022/8/10 15:54
 */
@Data
    public class ProjectVO {
    /**
     * yunji_project主键id
     */
    private Long id;
    /**
     * 项目id
     */
    private String projectId;
    /**
     * 项目名称
     */
    private String name;
    /**
     * 描述信息
     */
    private String description;
    /**
     * 操作人员
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String operator;
    private String createUser;
    private String updateUser;
    private Timestamp createTime;
    private Timestamp updateTime;
    private Long interfaceCount;
    /**
     * falcon项目id
     */
    private String falconProjectId;
    /**
     * 额外数据
     */
    private ProjectExtraData extraData;

    public ProjectDTO toAddDto(String empId) {
        AmapPreconditions.checkArgumentForUser(name != null, "name 不能为空");
        ProjectDTO dto = new ProjectDTO();
        dto.setProjectId(Strings.isBlank(projectId) ? DigestUtils.md5DigestAsHex(name.getBytes()) : projectId);
        dto.setName(name);
        dto.setDescription(description);
        dto.setFalconProjectId(falconProjectId);
        dto.setCreateUser(empId);
        dto.setUpdateUser(empId);
        dto.setExtraData(extraData);
        return dto;
    }

    public UpdateWrapper<ProjectDTO> toUpdateWrapper(String empId) {
        AmapPreconditions.checkArgumentForUser(projectId != null, "projectId 不能为空");
        UpdateWrapper<ProjectDTO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("project_id", projectId);
        if (Strings.isNotBlank(description)) {
            updateWrapper.set("description", description);
        }
        if (Strings.isNotBlank(empId)) {
            updateWrapper.set("update_user", empId);
        }
        if (Strings.isNotBlank(falconProjectId)) {
            updateWrapper.set("falcon_project_id", falconProjectId);
        }
        if (extraData != null) {
            updateWrapper.set("extra_data", JsonUtils.toString(extraData));
        }
        return updateWrapper;
    }

    public static ProjectVO fromDto(ProjectDTO dto, Map<String, Long> projectIdCountMap) {
        ProjectVO vo = new ProjectVO();
        vo.setId(dto.getId());
        vo.setProjectId(dto.getProjectId());
        vo.setName(dto.getName());
        vo.setDescription(dto.getDescription());
        vo.setCreateUser(dto.getCreateUser());
        vo.setUpdateUser(dto.getUpdateUser());
        vo.setCreateTime(dto.getCreateTime());
        vo.setUpdateTime(dto.getUpdateTime());
        vo.setFalconProjectId(dto.getFalconProjectId());
        vo.setInterfaceCount(projectIdCountMap.getOrDefault(dto.getProjectId(), 0L));
        vo.setExtraData(dto.getExtraData());
        return vo;
    }
}
